﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.PlayerActions;
using BuildPlannerLib.Terran.Buildings;
using BuildPlannerLib.Terran.Enums;
using BuildPlannerLib.Buildings;

namespace BuildPlannerLib.Terran.PlayerActions
{
    // Derived class from Action for using the Calldown: Scanner Sweep ability
    public class Scan : PlayerAction
    {
        // Field for the Command Center
        public IndividualCommandCenter CommandCenter;

        // Constructor with parameter for build
        public Scan(Build build)
        {
            Build = build;
            Description = "Use Calldown: Scanner Sweep";
            LongDescription = "Using the Orbital Command ability Calldown: Scanner Sweep.";
        }

        // Evaluates whether the player is Terran and whether there is at least one Orbital Command
        public override bool RequirementsFilled()
        {
            CommandCenter commandCenter = Build.Player.MainBuilding as CommandCenter;
            Error = null;

            if (commandCenter == null)
            {
                Error = "This ability can only be used by Terran players.";
                return false;
            }

            bool availableOrbitalCommand = commandCenter.IndividualMainBuildings.Exists(delegate(IndividualMainBuilding i)
            {
                IndividualCommandCenter c = i as IndividualCommandCenter;
                return c.UpgradeStatus == CommandCenterUpgrade.OrbitalCommand || c.UpgradeStatus == CommandCenterUpgrade.UpgradingToOC;
            });
            if (!availableOrbitalCommand)
            {
                Error = "No Orbital Commands available to complete this action.";
                return false;
            }

            return base.RequirementsFilled();
        }

        // Evaluates if there is an Orbital Command with enough energy
        public override bool StartCheck()
        {
            CommandCenter commandCenter = Build.Player.MainBuilding as CommandCenter;

            bool availableOrbitalCommand = commandCenter.IndividualMainBuildings.Exists(delegate(IndividualMainBuilding i)
            {
                IndividualCommandCenter c = i as IndividualCommandCenter;
                return !c.Floating && c.Energy.Amount >= 50;
            });
            if (!availableOrbitalCommand)
                return false;

            return base.StartCheck();
        }

        // Spends the Orbital Command energy
        public override void StartAction()
        {
            CommandCenter commandCenter = Build.Player.MainBuilding as CommandCenter;

            base.StartAction();

            CommandCenter = commandCenter.IndividualMainBuildings.Find(delegate(IndividualMainBuilding i)
            {
                IndividualCommandCenter c = i as IndividualCommandCenter;
                return !c.Floating && c.Energy.Amount >= 50;
            }) as IndividualCommandCenter;
            CommandCenter.Energy.Change(-50);
        }
    }
}
